#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Cleanup                                                            #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 40
#
# MANUAL: This script will delete most files in the current directory, leaving only the essential image processing results (APH files) and a few other files behind. This will save hard-drive space. There are two levels of cleanliness...
#
# RESET: merge_cleanup_location = "0"
# RESET: merge_cleanup_merge = "0"
# RESET: merge_cleanup_level = "4"
# RESET: merge_cleanup_mask = "n"
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set imagename = ""
set merge_cleanup_location = ""
set merge_cleanup_merge = ""
set merge_cleanup_level = ""
set merge_cleanup_mask = ""
#
#$end_vars
#
set scriptname = 2dx_cleanup
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
echo "<<@progress: 1>>"
#
set oldir = $PWD
#
if ( ${merge_cleanup_merge} == "0" ) then
  #
  if ( ${merge_cleanup_level} == "2" ) then
    ############################################################################# 
    ${proc_2dx}/linblock "getting disk usage for local merge directory"
    #############################################################################
    #
    set duval = `\du -x -s . | cut -f 1`
    echo ":: ${duval} kB  ${oldir}" 
    #
  else
    if ( ${merge_cleanup_level} == "4" || ${merge_cleanup_level} == "5" ) then
      \rm -f final_map.mrc
    endif
    #
    ############################################################################# 
    ${proc_2dx}/lin "cleaning up local merge directory"
    #############################################################################
    #
    echo dummy > dummy.tmp
    echo dummy > fort.1
    echo dummy > ${imagename}.plt
    echo dummy > TMP.dummy
    echo dummy > TMP-dummy
    echo dummy > TMP9871.dat
    echo dummy > 2dx_calcpositions.dummy
    echo dummy > REFdummy.hkl  
    echo dummy > TMP1.spi
    echo dummy > TMP-1.tmp
    echo dummy > TMP-1.dat
    echo dummy > test1.map
    echo dummy > results.1.spi
    echo dummy > LOG.1
    echo dummy > 2dx_calcpositions.tmp
    echo dummy > dummy.TMP
    if ( ! -d LOGS ) then
      \mkdir LOGS
    endif
    echo dummy > LOGS/dummy.log
    #
    if ( "${merge_cleanup_level}" != "1" ) then
      #
      ${proc_2dx}/linblock "Radical Cleanup"
      #
      \rm -rf FFTIR
      \rm -rf PS
      \rm -rf RESULTS-MRC
      \rm -rf RESULTS-PS
      \rm -rf PRJ
      \rm -rf HKL
      \rm -rf TRASH
      if ( -d LOGS ) then
        echo dummy > LOGS/dummy.log
        \rm -rf LOGS/*.log
      endif
      #
      if ( -d proc ) then
        cd proc
        \rm -f 2dx_backup.com
        \rm -f 2dx_finalmerge.com
        \rm -f 2dx_generateMergeMap.com
        \rm -f 2dx_generateImageMaps.com
        \rm -f 2dx_merge.com
        \rm -f 2dx_refine.com
        \rm -f 2dx_refreshData.com
        \rm -f 2dx_initialize.com
        \rm -f 2dx_allspace.com
        \rm -f 2dx_applyCTF.com
        \rm -f 2dx_determineCTF.com
        \rm -f 2dx_fftrans.com
        \rm -f 2dx_findlat.com
        \rm -f 2dx_generateMAP.com
        \rm -f 2dx_generateSymMap.com
        \rm -f 2dx_getspots.com
        \rm -f 2dx_inventory.com
        \rm -f 2dx_refine_unbend1.com
        \rm -f 2dx_refine_unbend2.com
        \rm -f 2dx_refinespots.com
        \rm -f 2dx_unbend1.com
        \rm -f 2dx_unbend2.com
        \rm -f 2dx_evaluateLattice.com
        \rm -f 2dx_getDefocusTilt.com
        \rm -f 2dx_getLattice.com
        \rm -f 2dx_refinetilt.com
        \rm -f 2dx_maskCrystal.com
        \rm -f 2dx_copyMerge.com
        \rm -f 2dx_deleteDir.com
        \rm -f 2dx_DetermineSwitch.com
        \rm -f 2dx_MaximumLikelihood.com
        \rm -f 2dx_mergeCustom.com
        \rm -f 2dx_mergeRefine.com
        \rm -f 2dx_openDir.com
        \rm -f 2dx_reunbend.com
        \rm -f Custom-1.com
        \rm -f Custom-2.com
        \rm -f Custom-3.com
        cd ..
      endif
      #
      \rm -f ManualMasking-CCmap.mrc
      \rm -f ManualMasking-UnbendPlot.mrc
      \rm -f selectionList.dat
      #
    else
      #
      ${proc_2dx}/linblock "Slight Cleanup"
      #
    endif  
    #
    \rm -f SUMMARY
    \rm -f 2dx_calcposition-positions.dat
    \rm -f 2dx_calcposition-runfile.com
    \rm -f CTFPLOT.PS
    \rm -f HKLOUT
    \rm -f mask.dat
    \rm -f masked_image.dat
    \rm -f peaks_image_final.dat
    \rm -f peaks_image.dat
    \rm -f peaks_xy_final.dat
    \rm -f peaks_xy.dat
    \rm -f TMP-quadserch-1.mrc
    \rm -f phase.dat
    \rm -f average.dat
    \rm -f amp.dat
    \rm -f amp_LHpass.dat
    \rm -f 2dx_findlat.out
    \rm -f 2dx_lattilt.1.out
    \rm -f 2dx_tiltgeom.out
    \rm -f 2dx_tiltgeom2.out
    \rm -f HISTO.PS
    \rm -f CCPLOT.PS
    \rm -f avrg.hk
    \rm -f avrg.hkl
    \rm -rf SCRATCH
    echo "<<@progress: 25>>"
    \rm -rf THUMB
    echo "<<@progress: 50>>"
    \rm -rf CUT
    echo "<<@progress: 75>>"  
    #
    \rm -f *.tmp
    \rm -f tag_data
    \rm -f fort.*
    \rm -f TMP.*
    \rm -f TMP*.spi
    \rm -f TMP-*
    \rm -f TMP*.dat
    \rm -f test*.map
    \rm -f results.*
    \rm -f LOG.* 
    \rm -f 2dx_calcpositions.*
    \rm -f REF*.hkl
    \rm -f TLTASM
    #
    echo "<<@progress: 20>>" 
    #
    ############################################################################# 
    ${proc_2dx}/linblock "Merge cleaned."
    #############################################################################
    #
    sleep 1
    # \rm -rf LOGS
    #
  endif
  #
  ${proc_2dx}/linblock "-"
  #
endif
#
if ( ${merge_cleanup_location} == "0" || ${merge_cleanup_location} == "1" ) then
  #
  if ( ${merge_cleanup_location} == "0" ) then
    cd ${oldir}
    cd ..
    find . -name 2dx_image.cfg -print | sed 's/\/2dx_image.cfg//' > ${oldir}/2dx_merge_alldirs.dat
    cd ${oldir}
  else
    cd ${oldir}
    cp 2dx_merge_dirfile.dat ${oldir}/2dx_merge_alldirs.dat
  endif  
  #
  set dirnum = `cat 2dx_merge_alldirs.dat | wc -l`
  echo ": Will work on ${dirnum} image directories."
  set increment = `echo ${dirnum} | awk '{ s = 70.0 / ( $1 + 1 ) } END { print s }'`
  set curval = 20.0  
  #
  set dircounter = ${dirnum}
  while ( ${dircounter} > 0 )
    #
    ${bin_2dx}/2dx_getline.exe << eot > TMP.tmp
${oldir}/2dx_merge_alldirs.dat
${dircounter}
eot
    #
    if ( ${merge_cleanup_location} == "1" ) then
      set from_dir = `cat TMP.tmp`
    else
      set from_dir = `cat TMP.tmp | cut -c3-`
    endif
    \rm TMP.tmp
    #
    cd ${oldir}
    cd ..
    cd ${from_dir}
    #
    set fullpath = `pwd`
    #
    if ( "${merge_cleanup_level}" == "2" ) then
      set duval = `\du -x -s . | cut -f 1`
      echo ":: ${duval} kB  ${from_dir}" 
    endif
    #
    if ( ${merge_cleanup_level} != "2" ) then
      ############################################################################# 
      ${proc_2dx}/linblock "cleanup ${from_dir}"
      #############################################################################
      #
      set level = "radical"
      set imagename = `cat 2dx_image.cfg | grep "set imagename =" | cut -d\" -f2`
      set nonmaskimagename = `cat 2dx_image.cfg | grep "set nonmaskimagename =" | cut -d\" -f2`
      #
      source ${proc_2dx}/2dx_cleanup_sub.com
      #
      if ( ${merge_cleanup_mask} == "y" ) then
        \rm -f ${nonmaskimagename}_mask.mrc
        \rm -f ${nonmaskimagename}_automask.mrc
        \rm -f ${nonmaskimagename}_automask_mask.mrc
      endif
      #
      if ( "${merge_cleanup_level}" == "3" || "${merge_cleanup_level}" == "4" ) then
        \rm -f final_map.mrc
      endif
      if ( "${merge_cleanup_level}" == "4" ) then
        \rm -f 2dx_image.cfg-backup1
        \rm -f 2dx_image.cfg-backup2
        \rm -f 2dx_image.cfg-backup3
      endif
    endif
    cd ${oldir}
    #
    set curval = `echo ${curval} ${increment} | awk '{ s = $1 + $2 } END { print s }'`
    set curprogress = `echo ${curval} | awk '{ s = int( $1 ) } END { print s }'`
    echo "<<@progress:${curprogress}>>"
    #
    @ dircounter -= 1
    cd ${oldir}
  end
  #
endif
#
echo "<<@progress: 100>>"
#
exit
#
# to display script in GUI:
source ${proc_2dx}/2dx_cleanup_sub.com
#
